package is.web.htgen.single;

import is.bean.region.*;
import is.web.Checker;
import is.web.Locale;
import java.rmi.RemoteException;
import javax.ejb.FinderException;

/**
 * CRUD form generator
 *
 * @author aspr0
 */
public class RegionForm extends AbstractForm
{
    private final String rowsText =
            "<tr>"
            + "<td>Id</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Name</td>"
            + "<td>%s</td>"
            + "</tr>";
    private final String rowsSelect =
            "<tr>"
            + "<td>Parent region</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>";
    private RegionBeanRemote bean;
    private RegionBeanRemoteHome regionHome;
    private Select<Region, RegionBeanRemote> select = new Select<Region, RegionBeanRemote>();

    public void setBean(RegionBeanRemote bean)
    {
        this.bean = bean;
    }

    public void setRegionHome(RegionBeanRemoteHome regionHome)
    {
        this.regionHome = regionHome;
    }

    public void setSelect(Select<Region, RegionBeanRemote> select)
    {
        this.select = select;
    }

    public String genCopyUpdate(boolean isUpdate) throws RemoteException, FinderException
    {
        Region region = bean.getData();

        Integer id = region.getId();
        String inputName = String.format(inputText, "name", region.getName());
        Integer parentId = region.getParentId();

        select.setNullAllowed(true);
        select.setSelectName("parentId");
        select.setSelectedId(parentId);
        if (isUpdate) 
        {
            select.setRemotes(regionHome.findParentsAvailable(id));
        }
        else
        {
            select.setRemotes(regionHome.findByExample(new Region()));
        }
        String parentSelect = select.generate();

        String rowsTextFilled = String.format(rowsText, id, inputName);
        String rowsSelectFilled = String.format(rowsSelect, parentSelect);
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);

        String links = (isUpdate)
                ? String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkCopy + lD + linkDelete)
                : String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkDelete);
        links = String.format(links, id);

        return String.format(form, tableFilled + inputSubmit) + links;
    }

    public String genCreate() throws FinderException, RemoteException
    {
        String inputHiddenParam = String.format(inputHidden, "create", "create");
        String inputName = String.format(inputText, "name", "");
        String rowsTextFilled = String.format(rowsText, "", inputName);

        select.setNullAllowed(true);
        select.setSelectName("parentId");
        select.setRemotes(regionHome.findByExample(new Region()));
        String parentSelect = select.generate();

        String rowsSelectFilled = String.format(rowsSelect, parentSelect);
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);

        return String.format(form, inputHiddenParam + tableFilled + inputSubmit);

    }

    public String genDelete() throws RemoteException
    {
        Region region = bean.getData();

        Integer id = region.getId();
        String name = region.getName();

        String rowsTextFilled = String.format(rowsText, id, name);
        String tableFilled = String.format(table, rowsTextFilled);

        String links = String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkCopy);
        links = String.format(links, id);

        return String.format(form, tableFilled + inputSubmit) + links;
    }

    public String genRetrieve() throws RemoteException, FinderException
    {
        Region region = bean.getData();

        Integer id = region.getId();
        String name = region.getName();
        Integer parentId = region.getParentId();


        String employeeLinkCanvas = "<a href=\"../region?id=%s&act=retrieve\">%s</a>";
        String parentLink = (Checker.isPositiveInteger(parentId))
                ? String.format(employeeLinkCanvas,
                                parentId,
                                regionHome.findByPrimaryKey(parentId).getData().getName())
                : Locale.NULL_READABLE;

        String rowsTextFilled = String.format(rowsText, id, name);
        String rowsSelectFilled = String.format(rowsSelect, parentLink);
        String links = String.format(linksDiv, linkTable + lD + linkUpdate + lD + linkCopy + lD + linkDelete);
        links = String.format(links, id);

        return String.format(table, rowsTextFilled + rowsSelectFilled) + links;
    }
}
